package com.qf.hsr.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/16
 **/
@Controller
public class Demo02Controller {

    @RequestMapping("/test21")
    public String test01(HttpServletResponse response){
        System.out.println("Demo02Controller test01...");
        Cookie cookie = new Cookie("username","tom");
        response.addCookie(cookie);
        return "success";
    }

    /**
     * @CookieValue (name = "username", required = false, defaultValue = "") String cookieValue
     *      获取指定名称的cookie值
     *      name : 指定cookie的名称
     *      required ：是否必须，false即不是必须的
     *      defaultValue : 没传cookie名称时，给一个默认值
     * @param cookieValue
     * @return
     */
    @RequestMapping("/test22")
    public String test02(@CookieValue(name = "username", required = false, defaultValue = "") String cookieValue){
        System.out.println("---> " + cookieValue);
        return "success";
    }

}
